<?php

namespace App\Admin\Controllers;

use App\Library\Util\ToolUtil;
use App\Models\Tool\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\Carbon;

class ToolUserController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '微信用户';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new User());

        $grid->header(function ($query) {
            $query = User::query();
            $yQuery = $query->clone();
            $total = $query->count();
            $tpl = 'Y-m-d H:i:s';
            $startTime = Carbon::today()->startOfDay()->format($tpl);
            $endTime = Carbon::today()->endOfDay()->format($tpl);
            $today = $query->whereBetween('create_time',[$startTime,$endTime])->count();
            $overview = "今日新增【{$today}】".PHP_EOL;

            $startTime = Carbon::yesterday()->startOfDay()->format($tpl);
            $endTime = Carbon::yesterday()->endOfDay()->format($tpl);
            $yesterday = $yQuery->whereBetween('create_time',[$startTime,$endTime])->count();
            $overview .= "昨日新增【{$yesterday}】".PHP_EOL;
            $overview .= "总用户【{$total}】";
            return "<pre>$overview</pre>";
        });

        $grid->column('id', __('ID'));
        $grid->column('nickname', __('昵称'));
        $grid->column('points', __('积分'));
        $grid->column('avatar', __('头像'))->image();
        $grid->column('openid', __('OPENID'));
        //$grid->column('session_key', __('Session key'));
        //$grid->column('province', __('Province'));
        //$grid->column('city', __('City'));
        //$grid->column('country', __('Country'));
        $grid->column('client_type', __('注册来源'))->display(function ($value) {
            if ($value == 'tool') {
                return "语珞时光机";
            }
            return "磁力集合";
        });
        $grid->column('last_time', __('最后登录时间'));
        $grid->column('last_ip', __('最后登录IP'))->display(function ($value) {
            return ToolUtil::getInstance()->getIpRegion($value);
        });
        //$grid->column('state', __('State'));
        //$grid->column('is_del', __('Is del'));
//        $grid->column('create_time', __('Create time'));
        $grid->column('update_time', __('更新时间'));
        $grid->model()->orderByDesc('id');
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(User::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('nickname', __('Nickname'));
        $show->field('points', __('Points'));
        $show->field('avatar', __('Avatar'));
        $show->field('openid', __('Openid'));
        $show->field('session_key', __('Session key'));
        $show->field('province', __('Province'));
        $show->field('city', __('City'));
        $show->field('country', __('Country'));
        $show->field('client_type', __('Client type'));
        $show->field('last_time', __('Last time'));
        $show->field('last_ip', __('Last ip'));
        $show->field('state', __('State'));
        $show->field('is_del', __('Is del'));
        $show->field('create_time', __('Create time'));
        $show->field('update_time', __('Update time'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new User());

        $form->text('nickname', __('Nickname'));
        $form->number('points', __('Points'))->default(50);
        $form->image('avatar', __('Avatar'));
        $form->text('openid', __('Openid'));
        $form->text('session_key', __('Session key'));
        $form->text('province', __('Province'));
        $form->text('city', __('City'));
        $form->text('country', __('Country'));
        $form->text('client_type', __('Client type'));
        $form->text('last_time', __('Last time'));
        $form->text('last_ip', __('Last ip'));
        $form->switch('state', __('State'))->default(1);
        $form->switch('is_del', __('Is del'));
        $form->datetime('create_time', __('Create time'))->default(date('Y-m-d H:i:s'));
        $form->datetime('update_time', __('Update time'))->default(date('Y-m-d H:i:s'));

        return $form;
    }
}
